from odoo import models, fields, api, _
from odoo.exceptions import UserError, ValidationError


# 审批流类
class Workflow(models.Model):
    _inherit = 'crm.lead'
    sale_order_ids = fields.One2many('sale.order', 'opportunity_id', string="Sale Order")
    s_application_id = fields.Many2one('application.product', string="PCB Application")
    sale_amount_total = fields.Monetary(store=True)
    quotation_count = fields.Integer(store=True)
    sale_order_count = fields.Integer(store=True)
    s_document_ids = fields.One2many('documents.document', 'res_id', string='Documents',
                                     domain="[('res_model', '=', 'sale.order'), ('res_id', '=', id)]")
    team_id = fields.Many2one(related='user_id.sale_team_id', string='Sales Team', store=True)
    # 创建客户
    def create_customer(self):
        action = self.env.ref('sb_sale.action_sb_partner_form_t')
        return action.read()[0]

    # 下单按钮
    def place_an_order(self):
        if not self.stage_id.is_won:
            raise ValidationError(_('Current offer clue not won'))
        if self.partner_id.is_virtual_customer:
            raise ValidationError(_('Please change the customer to official customer first!😏'))
        return {
            'name': _('Select Order type'),
            'type': 'ir.actions.act_window',
            'res_model': 'order.type.select.wizard',
            'view_mode': 'form',
            'target': 'new',
            'context': {
                'application_product_id': self.s_application_id.id,
                'pn': self.name,
                'customer_id': self.partner_id.id,
                'crm_id': self.id,
                'contact': self.delivery.id,
                'new_pn_rev': self.rev,
                'qty': self.quotation_lines[0].qty if self.quotation_lines else False,
                'nre': self.quotation_lines[0].nre if self.quotation_lines else False,
                'special_type': self.env.context.get('special_type', False),
                's_simi_fg': self.s_simi_fg
            },
        }

    def action_view_sale(self):
        return {
            'name': 'Sale Orders',
            'type': 'ir.actions.act_window',
            'res_model': 'sale.order',
            'view_mode': 'tree,form',
            'target': 'current',
            'domain': [('id', 'in', self.sale_order_ids.ids)],
        }

    def action_open_document_upload_wizard(self):
        self.ensure_one()
        return {
            'type': 'ir.actions.act_window',
            'name': 'Upload Document',
            'res_model': 'document.upload.wizard',
            'view_mode': 'form',
            'target': 'new',
            'context': {
                'default_res_model': 'crm.lead',
                'default_res_id': self.id,
                'default_folder_id': self.with_context(lang='en_US').env['documents.folder'].search(
                    [('name', '=', 'Quote')]).id,
                'default_partner_id': self.partner_id.id,
                'default_tag_id': self.with_context(lang='en_US').env['documents.tag'].search(
                    [('name', '=', 'Quote Information')]).id
            },
        }

    def action_view_documents(self):
        self.ensure_one()
        return {
            'name': 'Documents',
            'type': 'ir.actions.act_window',
            'res_model': 'documents.document',
            'view_mode': 'kanban,tree,form',
            'domain': [
                ('res_model', '=', 'crm.lead'),
                ('res_id', '=', self.id),
            ],
            'context': {
                'default_res_model': 'crm.lead',
                'default_res_id': self.id,
                'create': False,
                'searchpanel_default_folder_id': self.with_context(lang='en_US').env['documents.folder'].search(
                    [('name', '=', 'Quote')]).id,
            },
            'target': 'current',
        }
